package at.cookieforensics;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.File;
import java.lang.System;

/**
 * This class gives information about the browsers which are available
 * 
 * @author martin grottenthaler, markus stoeckler, lukas wabro
 * @version 2.0
 *
 */
public class FindBrowser {
	/** String that contains the operating system, the version and the architecture */
	private String [] OS = new String[3];
	/** Process for registry search requests */
	private Process search=null;
	/** BufferedReader for input responses */
	private BufferedReader input=null;
	/** String for temporary input lines */
	private String line=null;
	/** Boolean for installed or not installed */
	private Boolean chrome=false, firefox=false, ie=false;
	/** String for the version of the browser */
	private String firefoxversion=null, chromeversion=null, ieversion=null;
	/** CookieLogger instance for logging */
	CookieLogger cl = CookieLogger.getInstance();
	
	/**
	 * Constructor for the class FindBrowser.
	 * <br>Decides if the operating system is Linux or Windows.
	 * @param OS contains the operating system, the version and the architecture
	 */
	public FindBrowser(String[] OS){
		this.OS = OS;
		if(OS[0].contains("Linux")){
			ie=false;
			ieversion="0";
			Linux();			
		}
		else if (OS[0].contains("Windows")){
			ie=true;
			Windows();
		}
	}
	
	/*
	 * Operating System Windows
	 */
	/**
	 * Checks on Windows platforms if the Internet Explorer, Firefox or Chrome is installed
	 * <br>and reads the version if possible.
	 * 
	 * @exception Exception if a Browser is not installed or the version is not readable
	 */
	private void Windows(){
		/*
		 * Windows 7 or 8 with x64 Architecture
		 */
		if(OS[0].equals("Windows 7")&&OS[2].contains("64")||OS[0].equals("Windows 8")&&OS[2].contains("64")){
			String programFilesx86=System.getenv("ProgramFiles(x86)"); 
			//Log information
			cl.getLogger().info("Internet Explorer found");
			//read InternetExplorer Version from Registry
			ProcessBuilder builder = new ProcessBuilder ("reg", "query", "\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\"", "/v", "Version");
			Process p;
			try {
				p = builder.start();
				
				Scanner scanner = new Scanner(p.getInputStream());
				while (scanner.hasNext()){
					ieversion = scanner.next();
				}
				scanner.close();
				p.destroy();
			} catch (Exception e) {
				cl.getLogger().severe("Internet Explorer not found");
			}
			//check if firefox is installed
			try {
				File f=new File(programFilesx86+"\\Mozilla Firefox\\firefox.exe");
				if(f.exists() && !f.isDirectory()){
					firefox=true;
					//Log information
					cl.getLogger().info("Firefox found");
				}
			} catch (Exception e) {
				cl.getLogger().severe("Firefox not found");
				firefox=false;
			}
			//read firefox version
			if(firefox){
				try {
					search = Runtime.getRuntime().exec(programFilesx86+"\\Mozilla Firefox\\firefox.exe -v");
					input=new BufferedReader(new InputStreamReader(search.getInputStream()));
					
					while ((line = input.readLine()) != null) {
						firefoxversion = line.substring(16);
					}
					input.close();
				} catch (Exception e) {
					cl.getLogger().severe("error while reading firefox versionnumber");
				}
			}
			//check if chrome is installed
			try{
				File f=new File(programFilesx86+"\\Google\\Chrome\\Application\\chrome.exe");
				if(f.exists() && !f.isDirectory()){
					chrome=true;
					//Log information
					cl.getLogger().info("Chrome found");
				}
			} catch (Exception e) {
				cl.getLogger().severe("Chrome not found");
				chrome=false;
			}
			//read chrome version from VisualelementsManifest.xml
			if(chrome){
				try {
					input = new BufferedReader(new FileReader(programFilesx86+"\\Google\\Chrome\\Application\\VisualElementsManifest.xml"));
					while(input.readLine()!=null){
						line=input.readLine();
						if(line.contains("Logo")){
							line=line.substring(12,16);
							chromeversion=line;
							input.close();
							break;
						}
					}
				} catch (Exception e) {
					cl.getLogger().severe("error while reading chrome versionnumber");
				}
			}
			//Log information
			cl.getLogger().info("Searching for Browsers done");
		}
		/*
		 * Windows XP or Windows 7/8 with x86 Architecture
		 */
		else if(OS[0].equals("Windows XP")||OS[2].contains("86")){
			String programFiles = System.getenv("ProgramFiles");
			//Log information
			cl.getLogger().info("Internet Explorer found");
			//read InternetExplorer Version from Registry
			ProcessBuilder builder = new ProcessBuilder ("reg", "query", "\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\"", "/v", "Version");
			
			Process p;
			try {
				p = builder.start();
				
				Scanner scanner = new Scanner(p.getInputStream());
				while (scanner.hasNext()){
					ieversion = scanner.next();
				}
				scanner.close();
				p.destroy();
			} catch (Exception e) {
				cl.getLogger().severe("Internet Explorer not found");
			}
			
			//check if firefox is installed
			try{
				File f=new File(programFiles+"\\Mozilla Firefox\\firefox.exe");
				if(f.exists() && !f.isDirectory()){
					firefox=true;
					//Log information
					cl.getLogger().info("Firefox found");
				}
			} catch (Exception e) {
				cl.getLogger().severe("Firefox not found");
				firefox=false;
			}
			//read firefox version
			if(firefox){
				try {
					search = Runtime.getRuntime().exec(programFiles+"\\Mozilla Firefox\\firefox.exe -v");
					input=new BufferedReader(new InputStreamReader(search.getInputStream()));
					
					while ((line = input.readLine()) != null) {
						firefoxversion = line.substring(16);
					}
					input.close();
				} catch (Exception e) {
					cl.getLogger().severe("error while reading firefox versionnumber");
				}
			}
			//check if chrome is installed
			try{
				File f=new File(programFiles+"\\Google\\Chrome\\Application\\chrome.exe");
				if(f.exists() && !f.isDirectory()){
					chrome=true;
					//Log information
					cl.getLogger().info("Chrome found");
				}
			} catch (Exception e) {
				cl.getLogger().severe("Chrome not found");
				chrome=false;
			}
			
			//read chrome version from Registry
			if(chrome){
				builder = new ProcessBuilder ("reg", "query", "\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome\"", "/v", "Version");
				try {
					p = builder.start();
					
					Scanner scanner = new Scanner(p.getInputStream());
					while (scanner.hasNext()){
						chromeversion = scanner.next();
					}
					scanner.close();
					p.destroy();
					
				} catch (Exception e) {
					cl.getLogger().severe("error while reading chrome versionnumber");
				}
			}
		}
		//Log information
		cl.getLogger().info("Searching for Browsers done");
	}
	/*
	 * Operating System Linux
	 */
	/**
	 * Checks on Linux platforms if Firefox or Chrome is installed
	 * <br>and reads the version if possible
	 * 
	 * @exception Exception if a Browser is not installed
	 */
	private void Linux(){
		//Log information
		cl.getLogger().severe("Internet Explorer not found");
		try {
			search = Runtime.getRuntime().exec("firefox --version");
			input=new BufferedReader(new InputStreamReader(search.getInputStream()));
			
			while ((line = input.readLine()) != null) {
		        if (line.contains("Firefox")) {
					firefox = true;
					firefoxversion = line.substring(16);
					//Log information
					cl.getLogger().info("Firefox found");
				}
			}
			input.close();
		} catch (Exception e) {
			cl.getLogger().severe("Firefox not found");
			firefox=false;
		}
		
		//check if chrome is installed
		try{	
			search = Runtime.getRuntime().exec("google-chrome --version");
			input=new BufferedReader(new InputStreamReader(search.getInputStream()));
			
			while ((line = input.readLine()) != null) {
		        if (line.contains("Chrome")) {
					chrome = true;
					chromeversion = line.substring(14);
					//Log information
					cl.getLogger().info("Chrome found");
				}
			}
			input.close();
		} catch (Exception e){
			cl.getLogger().severe("Chrome not found");
			chrome=false;
		}
		//Log information
		cl.getLogger().info("Searching for Browsers done");
	}
	/**
	 * Returns true if Firefox is installed
	 * @return true if firefox was found
	 */
	public Boolean foundFirefox(){
		return this.firefox;
	}
	/**
	 * Returns true if Chrome is installed
	 * @return true if chrome was found
	 */
	public Boolean foundChrome(){
		return this.chrome;
	}
	/**
	 * Returns true if internet explorer is installed
	 * @return true if internet explorer was found
	 */
	public Boolean foundInternetExplorer(){
		return this.ie;
	}
	/**
	 * Returns the version of the installed firefox
	 * @return version from installed firefox
	 */
	public String getFirefoxVersion(){
		return this.firefoxversion;
	}
	/**
	 * Returns the version of the installed chrome
	 * @return version from installed chrome
	 */
	public String getChromeVersion(){
		return this.chromeversion;
	}
	/**
	 * Returns the version of the installed internet explorer
	 * @return version from installed internet explorer
	 */
	public String getIEVersion(){
		return this.ieversion;
	}
}